/*
 * AnnotationInterpretor.java
 *
 * Created on 6 f�vrier 2008, 10:30
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.great.functionalCore;

import br.great.beans.*;
import org.geonames.WikipediaArticle;
import java.util.Calendar;
import java.util.Vector;
//import arcweb.address.Address;
import javax.swing.*;
import java.util.Iterator;

/**
 *
 * @author defautce
 */
public class AnnotationInterpretator implements Runnable {
    private Collection c;
    private javax.swing.JLabel labelPhoto;
    private javax.swing.JLabel labelProgression;
    private javax.swing.JProgressBar progressBar;
    private ProgressBarListener listener;
    private int progress = 1;
    private int typeCollection=0;
    
    
    /** Creates a new instance of AnnotationInterpretor */
    public AnnotationInterpretator() {
    }
    
    public void interprete(Collection c, JLabel photo, JLabel progression, JProgressBar bar, ProgressBarListener listener, int typeCollection){
        this.c = c;
        this.labelPhoto = photo;
        this.typeCollection=typeCollection;
        this.labelProgression = progression;
        this.progressBar = bar;
        this.listener = listener;
        Thread t=new Thread(this);
        t.start();
    }
    
    public void run(){
        Vector<Photo> photos = this.c.getPhotos();
        progressBar.setMinimum(0);
        progressBar.setMaximum((photos.size()*3)+1);
        progressBar.setIndeterminate(false);
        Iterator it = photos.iterator();
        int cont=0;
        while(it.hasNext()){
            cont++;
            Photo p = (Photo) it.next();
            createPhoto(p, this.typeCollection);
            labelPhoto.setText("photo : "+p.getName());
            System.out.println("Photo "+ cont+" interpretation");
            interpretationPhoto(p);
            System.out.println("End of Photo"+ cont+" interpretation");
            // insert friendInference
        }
        progressBar.setValue(progress++);
        this.listener.processEnded();
    }
    
    private void createPhoto(Photo p, int typeCollection){
        PhotoCreator creator = new PhotoCreator();
        creator.createPhoto(p, typeCollection);
    }
    
    private void interpretationPhoto(Photo p){
        labelProgression.setText("near by wikipedia articles...");
        Vector<Wiki> articles = WikipediaObjectDerivator.getDerivatedObject(p.getCoor());
        p.setNearByWikipediaObject(articles);
        progressBar.setValue(progress++);
        labelProgression.setText("address...");
        AddressDerivator addresDeriv=new AddressDerivator();
        Address ad = addresDeriv.deriveAddress(p.getCoor());
        p.setAddress(ad);
        progressBar.setValue(progress++);
        labelProgression.setText("season...");
        Calendar cal= Calendar.getInstance();
        if (p.getDateTime()!=null){
            cal.setTime(p.getDateTime());
            String s = SeasonCalculator.calculateSeason(cal.getTime(),p.getCoor().getLatitude());
            p.setSeason(s);
        }
        progressBar.setValue(progress++);
    }
    
}
